package com.anhry.app.safety.util.service;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

public class UserSessionCheck {

	private Logger logger = Logger.getLogger(UserSessionCheck.class);
	
	private static UserSessionCheck UserSessionCheck;

	public UserSessionCheck(HttpServlet servlet) {
	}

	public static UserSessionCheck getInstance(HttpServlet servlet) {
		if (UserSessionCheck == null) {
			UserSessionCheck = new UserSessionCheck(servlet);
		}
		return UserSessionCheck;
	}

	public boolean setSession(String userName, HttpServlet servlet,
			HttpSession session) {
		boolean isLoggedOn = false;
		final ServletContext sc = servlet.getServletContext();//取得容器,容器中根据用户唯一标识userID存放session  
		if(logger.isDebugEnabled())
		System.out.println(sc);
		System.out.println(session);
		if (sc.getAttribute(userName) != null) {
			// 第二次登陆后第一次无效  
			((HttpSession) sc.getAttribute(userName)).invalidate();// 清除第一次登陆的session  
			System.out.println(session);
		}
		sc.setAttribute(userName, session);//放入当前最新session  

		return isLoggedOn;
	}
}
